﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Linkmeme.Models;

namespace Linkmeme.Controllers
{
    public class UserController : BaseController
    {
        LinkmemeDataContext dataContext = new LinkmemeDataContext();

        public void SignUp()
        {
            RenderView("SignUp");
        }

        public void LogOut()
        {
            FormsAuthentication.SignOut();
            RedirectToAction("Index", "Home");
        }

        public void ChangePassword()
        {
            RenderView("ChangePassword");
        }

        public void SelectProviders()
        {

            SelectProvidersViewData viewdata = new SelectProvidersViewData();
            viewdata.AvailableProviders = dataContext.ContentProviders.ToList();
            viewdata.UserProviders = dataContext.GetProvidersByUser(CurrentUserId);
            RenderView("SelectProviders", viewdata);
        }

        private void AddProvider(int providerId, string userName)
        {

            UserContentProvider ucp = new UserContentProvider();
            ucp.ProviderID = providerId;
            ucp.ProviderUserName = userName;
            ucp.UserId = CurrentUserId;
            dataContext.UserContentProviders.InsertOnSubmit(ucp);
            dataContext.SubmitChanges();
        }

        public void RemoveProvider(int id)
        {
            UserContentProvider ucp = dataContext.UserContentProviders.Where(p => p.ID == id).Single();
            dataContext.UserContentProviders.DeleteOnSubmit(ucp);
            dataContext.SubmitChanges();

            RedirectToAction("SelectProviders");
        }

        public void CheckUser(int providerid, string username)
        { 
            JsonResult jr = new JsonResult();
            //if (username == "bbpnb")
            if (ProviderValidator.CheckUserFromProvider(providerid, username))
            {
                AddProvider(providerid, username);
                jr.isSuccessful = true;
            }
            else
            {
                jr.isSuccessful = false;
                jr.errorMessage = "该用户名不存在";
            }
            RenderView("Json", jr);
        }

        public void Search(string q)
        {
            var users = from u in dataContext.Users
                        where u.UserName.StartsWith(q)
                        select u;
            RenderView("SearchResult", users.ToList());
        }

        public void AddFriend(Guid userId)
        {
            JsonResult jr = new JsonResult();

            if (this.IsUserAuthenticated)
            {
                UserFriend uf = new UserFriend();
                uf.UserId = CurrentUserId;
                uf.FriendId = userId;
                uf.CreatedDate = DateTime.Now;
                dataContext.UserFriends.InsertOnSubmit(uf);
                dataContext.SubmitChanges();

                jr.isSuccessful = true;
            }
            else
            {
                jr.isSuccessful = false;
                jr.errorMessage = Server.HtmlEncode( "LoginFirst");
            }
            RenderView("Json", jr);

        }

        public void RemoveFriend(Guid userId)
        {
            JsonResult jr = new JsonResult();
            jr.isSuccessful = false;
            if (this.IsUserAuthenticated)
            {
                UserFriend uf = dataContext.UserFriends.Single(uu => uu.UserId == CurrentUserId && uu.FriendId == userId);
                if (uf != null)
                {
                    dataContext.UserFriends.DeleteOnSubmit(uf);
                    dataContext.SubmitChanges();
                    jr.isSuccessful = true;
                }
                else
                {
                    jr.isSuccessful = false;
                    jr.errorMessage = Server.HtmlEncode( "CantRemove");
                }

            }
            RenderView("Json", jr);

        }

        public void ViewFriend(Guid friendid)
        {
            //RenderView("");
        }
    }
}
